package com.inno.life.controller.controller;

import com.google.common.base.Preconditions;
import com.inno.life.common.context.LoginUserContext;
import com.inno.life.common.entity.CommonReturnType;
import com.inno.life.common.entity.PageResult;
import com.inno.life.controller.convert.MomentDTOConverter;
import com.inno.life.controller.convert.UserDTOConverter;
import com.inno.life.controller.dto.MomentDTO;
import com.inno.life.controller.dto.RecommendUserDTO;
import com.inno.life.domain.entity.MomentBO;
import com.inno.life.domain.entity.RecommendUserBO;
import com.inno.life.domain.service.MomentDomainService;
import com.inno.life.domain.utils.SensitiveWordUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sean
 * 动态controller
 */
@RestController
@RequestMapping("/life/moment")
@Slf4j
public class MomentController {
    @Resource
    private MomentDomainService momentService;
    @Resource
    private SensitiveWordUtil sensitiveWordUtil;

    @PostMapping("/add")
    public CommonReturnType<Void> add(@RequestBody MomentDTO momentDTO) {
        log.info("MomentController.add.momentDTO:{}", momentDTO);

        Preconditions.checkArgument(!StringUtils.isBlank(momentDTO.getContent()), "内容不能为空");
        sensitiveWordUtil.checkAndThrowFormatError(momentDTO.getContent());
        MomentBO momentBO = MomentDTOConverter.INSTANCE.convertDTOtoBO(momentDTO);
        Long userId = LoginUserContext.getUserId();
        momentBO.setUserId(userId);
        this.momentService.add(momentBO);
        return CommonReturnType.success();
    }

    @PostMapping("/queryById")
    public CommonReturnType<MomentDTO> queryById(@RequestBody MomentDTO momentDTO) {
        Preconditions.checkNotNull(momentDTO.getId(), "动态id不能为空");
        momentDTO.setCurrentUserId(LoginUserContext.getUserId());
        MomentBO momentBO = MomentDTOConverter.INSTANCE.convertDTOtoBO(momentDTO);
        MomentBO result = this.momentService.queryById(momentBO);
        MomentDTO dtoResult = MomentDTOConverter.INSTANCE.convertBOtoDTO(result);
        return CommonReturnType.success(dtoResult);
    }

    @PostMapping("/queryPage")
    public CommonReturnType<PageResult<MomentDTO>> queryPage(@RequestBody MomentDTO momentDTO) {
        Preconditions.checkNotNull(momentDTO.getPage(), "分页参数不能为空");
        Preconditions.checkNotNull(momentDTO.getPageSize(), "分页参数不能为空");
        MomentBO momentBO = MomentDTOConverter.INSTANCE.convertDTOtoBO(momentDTO);
        Long userId = LoginUserContext.getUserId();
        momentBO.setCurrentUserId(userId);
        PageResult<MomentBO> boResult = this.momentService.queryPage(momentBO);
        PageResult<MomentDTO> dtoResult = PageResult.of(MomentDTOConverter.INSTANCE.converBOListToDTOList(boResult.getList()), boResult.getTotal());
        return CommonReturnType.success(dtoResult);
    }

    @PostMapping("/del")
    public CommonReturnType<Void> del(@RequestBody MomentDTO momentDTO) {
        Preconditions.checkNotNull(momentDTO.getId(), "动态id不能为空");
        Long userId = LoginUserContext.getUserId();
        momentDTO.setCurrentUserId(userId);
        MomentBO momentBO = MomentDTOConverter.INSTANCE.convertDTOtoBO(momentDTO);
        this.momentService.del(momentBO);
        return CommonReturnType.success();
    }

    @PostMapping("/update")
    public CommonReturnType<Void> update(@RequestBody MomentDTO momentDTO) {
        Preconditions.checkNotNull(momentDTO.getId(), "动态id不能为空");
        Long userId = LoginUserContext.getUserId();
        momentDTO.setCurrentUserId(userId);
        MomentBO momentBO = MomentDTOConverter.INSTANCE.convertDTOtoBO(momentDTO);
        this.momentService.update(momentBO);
        return CommonReturnType.success();
    }

    /**
     * 推荐文章
     */
    @GetMapping("/recommend")
    public CommonReturnType<List<MomentDTO>> recommend() {
        List<MomentBO> boList = this.momentService.recommend();
        List<MomentDTO> momentDTOS = MomentDTOConverter.INSTANCE.converBOListToDTOList(boList);
        return CommonReturnType.success(momentDTOS);
    }

    /**
     * 作者榜
     */
    @GetMapping("/recommend-user")
    public CommonReturnType<List<RecommendUserDTO>> recommendUser() {
        List<RecommendUserBO> boList = this.momentService.recommendUser();
        List<RecommendUserDTO> dtoList= UserDTOConverter.INSTANCE.convertBOListToDTOList(boList);
        return CommonReturnType.success(dtoList);
    }
}
